<script>
    var jxAppLevelSelect = angular.module('JxApp.levelsel', ['ngSanitize']);

    /**************************************************
     * 分层级下拉框组件
     **************************************************/
    jxAppLevelSelect.directive('levelsel', function ($compile, $window, $sce) {
	    return {
		    restrict: 'A',
		    //transclude: true,
		    controller: function ($scope, $http) {

			    $scope.ngModel = 0;

			    $scope.settingOptions = {
				    id: 'id',
				    name: 'name',
				    children: 'lstSubHierarchyVO',
				    selVal: '',
				    addEmptyOption: true,
				    emptyVal: '',
				    emptyText: '请选择...'
			    };

			    $scope.createOption = function (settingOptions, obj, rowData, level) {
				    var spaceStr = "";
				    for (var i = 0; i < level; i++) {
					    spaceStr = "&nbsp;&nbsp;&nbsp;" + spaceStr;
				    }

				    $.each(rowData, function (i, item) {
					    if ($scope.selVal == item.pTableVO[settingOptions.id]) {
						    var element = $compile("<option ng-selected='true' value='" + item.pTableVO[settingOptions.id] + "'>" + spaceStr + item.pTableVO[settingOptions.name] + "</option>")($scope);
						    obj.append(element);
					    } else {
						    var element = $compile("<option value='" + item.pTableVO[settingOptions.id] + "'>" + spaceStr + item.pTableVO[settingOptions.name] + "</option>")($scope);
						    obj.append(element);
					    }
					    if (item[settingOptions.children] != undefined && item[settingOptions.children].length > 0) {
						    var itemLevel = level + 1;
						    $scope.createOption(settingOptions, obj, item[settingOptions.children], itemLevel);

					    }
				    });
			    };

			    $scope.createOptionArray = function (settingOptions, obj, rowData, level, desArray) {
				    var spaceStr = "";
				    for (var i = 0; i < level; i++) {
					    spaceStr = "　" + spaceStr;
				    }

				    $.each(rowData, function (i, item) {

					    if ($scope.selVal == item.pTableVO[settingOptions.id]) {
						    desArray.push({
							    'id': item.pTableVO[settingOptions.id],
							    'text': $sce.trustAsHtml(spaceStr + item.pTableVO[settingOptions.name]),
							    'level': level
						    });
					    } else {
						    desArray.push({
							    'id': item.pTableVO[settingOptions.id],
							    'text': $sce.trustAsHtml(spaceStr + item.pTableVO[settingOptions.name]),
							    'level': level
						    });
					    }
					    if (item[settingOptions.children] != undefined && item[settingOptions.children].length > 0) {
						    var itemLevel = level + 1;
						    $scope.createOptionArray(settingOptions, obj, item[settingOptions.children], itemLevel, desArray);

					    }
				    });
			    };

			    $scope.getTimes = function (n) {
				    return new Array(n);
			    };
		    },


		    scope: {
			    selVal: '=ngModel',
			    loptions: '=loptions'
		    },

		    compile: function compile(tElement, tAttrs, transclude) {
			    return {
				    pre: function preLink(scope, iElement, iAttrs, controller) {

					    var svg2 = $compile('<option value="@{{opt.id}}" ng-selected="@{{opt.id==selVal}}" ng-repeat="opt in options" ng-bind="opt.text"></option>')(scope);

					    scope.options = [];
					    iElement.append(svg2);

					    scope.$watch('loptions', function (newValue, oldValue) {
						    scope.options = [];
						    scope.createOptionArray(scope.settingOptions, iElement, scope.loptions, 0, scope.options);
					    });
				    },
				    post: function postLink(scope, iElement, iAttrs, controller) {
				    }
			    }
		    },

	    };
    });
</script>